package test3.demo1;

import test3.demo1.Student;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class test {
    public static void main(String[] args) {
        String info = "10001,张无忌,男,2023-07-22 11:11:12,东湖-黄鹤楼#10002,赵敏,女,2023-07-22 09:11:21,黄鹤楼-归元禅寺#10003,周芷若,女,2023-07-22 04:11:21,木兰文化区-东湖#10004,小昭,女,2023-07-22 08:11:21,东湖#10005,灭绝,女,2023-07-22 17:11:21,归元禅寺";

        // 业务一：解析字符串并创建Student对象
        List<Student> students = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        // 解析用户组
        String[] userGroups = info.split("#");
        for (String userGroup : userGroups) {
            String[] parts = userGroup.split(",");

            if (parts.length >= 5) {
                Long id = Long.valueOf(parts[0]);
                String name = parts[1];
                String gender = parts[2];
                LocalDateTime choiceTime = LocalDateTime.parse(parts[3], formatter);

                // 解析景点
                String[] destinations = null;
                if (parts.length > 4) {
                    String destStr = parts[4];
                    if (destStr.contains("-")) {
                        // 处理多个景点
                        destinations = destStr.split("-");
                    } else {
                        destinations = new String[]{destStr};
                    }
                }

                Student student = new Student(id, name, gender, choiceTime, destinations);
                students.add(student);
            }
        }

        // 业务二：统计每个景点的选择次数
        Map<String, Integer> destinationCount = new HashMap<>();
        for (Student student : students) {
            String[] destinations = student.getDestinations();
            for (String dest : destinations) {
                // 统计
                destinationCount.put(dest, destinationCount.getOrDefault(dest, 0) + 1);
            }
        }

        System.out.println("各景点选择次数统计：");
        for (Map.Entry<String, Integer> entry : destinationCount.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
        }

        // 业务三：找出选择人数最多的景点和未选择该景点的学生
        String mostPopularDestination = destinationCount.entrySet().stream()
                .max(Map.Entry.comparingByValue())
                .map(Map.Entry::getKey)
                .orElse(null);

        System.out.println("\n选择人数最多的景点：" + mostPopularDestination);

        System.out.println("未选择该景点的学生：");
        for (Student student : students) {
            boolean selected = false;
            for (String dest : student.getDestinations()) {
                if (dest.equals(mostPopularDestination)) {
                    selected = true;
                    break;
                }
            }
            if (!selected) {
                System.out.println("姓名：" + student.getName() + ", ID：" + student.getId());
            }
        }
    }
}

